/**
 * zmn产品模块-产品列表
 * @author tanghongjun
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        // defaultToolbar: json.hasExportExcel ? ['filter', 'exports'] : ['filter'], done: function () {
        defaultToolbar: ['filter', 'exports'], done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        },
        page: true,
        limit: json.pageSize,
        height: 'full-189'
    });
    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    // // 启用/停用
    // table.on("toolbar(zmn-table)", function (obj) {
    //     if (obj.event != "start" && obj.event != "stop") {
    //         return;
    //     }
    //
    //     zmnPage.layStartStop(obj, "/showProduct/updateStatus.action", ids.join(","));
    // });

    table.on("toolbar(zmn-table)", function (obj) {
        switch (obj.event) {
            case 'CUSTOM_EXPORT':
                $("#query-export").click();
                return;
            case 'stop':
            case 'start':
                if (ids.length === 0) {
                    return layer.alert("请选择至少1个产品！");
                }
                zmnPage.layStartStop(obj, "/showProduct/updateStatus.action", ids.join(","));
            default: return;
        }
    });

    form.on('submit(query-export)', function (data) {
        let index = layer.load(1, {
            shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
        });
        zmnUtil.download("/showProduct/outputExcel.action?" + $("#show_product_list").serialize(), "前台产品报表.xlsx", function () {
            layer.close(index);
        });
        return false;
    });

    zmnProduct.laySelectSpecial(form, "/showProduct/listChilds.action", "/showProduct/listCateChilds.action", "servCategId", "categOneId", "categId");

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.productId);
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        zmnPage.laySort(obj, "/showProduct/updateSort.action", obj.data.productId);
    });

    form.on("switch(chk_status)", function (obj) {
        console.log(obj);
        let url = "/showProduct/updateStatus.action";
        let productId = obj.elem.getAttribute("key");
        let status = obj.elem.checked ? 2 : 1;
        console.log(status);
        switch (status) {
            case 1:
                zmnPage.ajaxSimple(url, {ids: productId, status: 1}, 1000);
                break;
            case 2:

                zmnPage.ajaxSimple(url, {ids: productId, status: 2}, 1000);
                break;
        }
        setTimeout(function () {
            location.reload(true);
        }, 3000);
    });

    //对外暴露的接口
    exports('product_show_list_js', {});
});

var zmnProduct = {
    /**
     * 多级下拉框联动
     * @param form layui.form
     * @param url1 selectOneId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param url1 selectTwoId请求下级数据的url，返回数据格式 [[value,text], [value,text], [value,text], …]
     * @param selectOneId 第1级select的ID，不含#号
     * @param selectTwoId 第2级select的ID，不含#号
     * @param selectThrId 第3级select的ID，不含#号，如无第3级则两级联动
     */
    laySelectSpecial: function (form, url1, url2, selectOneId, selectTwoId, selectThrId) {
        let $ = layui.$;
        let servCategId;
        let categOneId;
        form.on("select(" + selectOneId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            servCategId = obj.value;
            $.ajax({
                type: "GET", url: url1,
                dataType: "json", data: {parentId: servCategId},
                success: function (items) {
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectTwoId).html(optionHtml);
                    form.render("select");
                }
            });
        });
        if (zmnUtil.isEmptyNull(selectThrId)) {
            return;
        }
        ;

        form.on("select(" + selectTwoId + ")", function (obj) {
            if (zmnUtil.isNull(obj) || obj.value == 0) {
                return;
            }
            categOneId = obj.value;
            servCategId = $('#servCategId').val();
            ;
            $.ajax({
                type: "GET", url: url2,
                dataType: "json", data: {categOneId: obj.value, servCategId: servCategId},
                success: function (items) {
                    var optionHtml = '<option value="0">选择</option>';
                    for (var i = 0; i < items.length; i++) {
                        optionHtml += '<option value="' + items[i][0] + '">' + items[i][1] + '</option>';
                    }
                    $("#" + selectThrId).html(optionHtml);
                    form.render("select");
                }
            });
        });
    },
}

//显示描述
function show_tips(flag, tips, obj){
    //小tips
    //如果没有权限则提示
    if (!flag) {
        layer.tips(tips,obj,{
            tips:[1,'#3595CC'],
            time:3000
        })
    }
}
